using System.ComponentModel;
using System.Text.RegularExpressions;

namespace VSSDKAssist.Assets.Converters
{
	/// <summary>
	/// TypeConverter for a file extension
	/// </summary>
    public class FileExtensionConverter : StringConverter
	{
        private Regex validExtension = new Regex(@"(^\.[a-zA-Z]+$)|(^\.\*$)",
											 RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

		/// <summary>
		/// Returns whether the given value object is valid for this type and for the specified context.
		/// </summary>
		/// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
		/// <param name="value">The <see cref="T:System.Object"></see> to test for validity.</param>
		/// <returns>
		/// true if the specified value is valid for this object; otherwise, false.
		/// </returns>
		public override bool IsValid(ITypeDescriptorContext context, object value)
		{
			bool response = false;
			string file = value as string;

			if(file != null)
			{
                response = validExtension.IsMatch(file);
			}

			return response;
		}
	}
}
